layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    
    laydate.render({
    	elem:'#startTime'
    });
    
    laydate.render({
    	elem:'#endTime'
    });

    /**
     * 云仓补货表管理
     */
    var ToolRepairGoods = {
        tableId: "toolRepairGoodsTable"
    };

    /**
     * 初始化表格的列
     */
    ToolRepairGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'sourcewarehouseName', sort: true, title: '源仓库名称'},
            {field: 'sourceareaNumber', sort: true, title: '源库区编号'},
            {field: 'sourcepositionNumber', sort: true, title: '源库位编号'},
            {field: 'num', sort: true, title: '补货数量'},
            {field: 'aimwarehouseName', sort: true, title: '目的仓库名称'},
            {field: 'aimareaNumber', sort: true, title: '目的库区编号'},
            {field: 'aimpositionNumber', sort: true, title: '目的库位编号'},
            {field: 'goodsName', sort: true, title: '商品名'},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'nodes', sort: true, title: '备注'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createName', sort: true, title: '创建用户'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    ToolRepairGoods.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['sourcewarehouseId'] = $("#sourcewarehouseId").val();
        queryData['aimwarehouseId'] = $("#aimwarehouseId").val();
        queryData['sourcepositionNumber'] = $("#sourcepositionNumber").val();
        queryData['condition'] = $("#condition").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        queryData['goodsName'] = $("#goodsName").val();
        table.reload(ToolRepairGoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    ToolRepairGoods.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/toolRepairGoods/add';
    };

    /**
     * 导出excel按钮
     */
    ToolRepairGoods.exportExcel = function () {
        var checkRows = table.checkStatus(ToolRepairGoods.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ToolRepairGoods.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/toolRepairGoods/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ToolRepairGoods.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/toolRepairGoods/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ToolRepairGoods.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };
	    
	//批量删除
    $('#btnRevoke').click(function () {
    	var checkStatus = table.checkStatus('toolRepairGoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/toolRepairGoods/btnRevoke?ids="+ids, function (data) {
	                if(data.success){
	                	Feng.success("删除成功!");
	                	table.reload(ToolRepairGoods.tableId);
	                } else {
	                    Feng.error("删除失败!" + data.message + "!");
	                }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
		} else {
			layer.alert('请至少选中一条数据');
		}
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ToolRepairGoods.tableId,
        url: Feng.ctxPath + '/toolRepairGoods/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: ToolRepairGoods.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ToolRepairGoods.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ToolRepairGoods.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ToolRepairGoods.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ToolRepairGoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ToolRepairGoods.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ToolRepairGoods.onDeleteItem(data);
        }
    });
});
